package com.summer.controller;


import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbActivityinfo;
import com.summer.entity.TbApply;
import com.summer.entity.TbUserinfo;
import com.summer.mapper.TbActivityinfoMapper;
import com.summer.mapper.TbApplyMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *测试通过20:27
 * @author summer502
 * @since 2021-03-31
 */
@CrossOrigin
@RestController
@RequestMapping("/tb-apply")
@Transactional
@Api(value = "TbApplyController", description = "活动审核api")

public class TbApplyController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired(required =false)
    TbApplyMapper tbApplyMapper;
    /**
    private Integer applyId;
    private Integer activityId;
    private Integer userId;
    private Integer isOpen;
    private String auditStatus;
    private String createUser;
    private String updateUser;
    private Date create_time;
    private Date update_time;
    private Integer isDelete;
     */

    /**
     * 超级管理员添加活动审核
     * @param activityId
     * @param userId
     * @param isOpen
     * @param auditStatus
     * @param createUser
     * @param updateUser
     * @param Authorization
     * @return
     * @throws IOException
     * @throws TokenExpiredException
     */
    @ApiOperation("超级管理员添加活动审核")
    @PostMapping("/teacher/addTbApply")
    @ResponseBody
    public ResponseEntity addTbApply(@RequestParam(required = false) int activityId,
                                          @RequestParam(required = false) int userId,
                                          @RequestParam(required = false) int isOpen,
                                          @RequestParam(required = false) String auditStatus,
                                          @RequestParam(required = false) String createUser,
                                          @RequestParam(required = false) String updateUser,
                                          @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        if (tbUserinfo.getUserId()!=1&&tbUserinfo.getUserId()>0){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","权限错误");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
        //根据user_jobNumber查询数据库是否有该用户存在，不存在则注册，存在则注册失败
        QueryWrapper<TbApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activity_id", activityId);
        TbApply tbApply = tbApplyMapper.selectOne(queryWrapper);
        if (tbApply == null){
            responseEntity = ResponseEntity.ok();
            TbApply tbApply1 = new TbApply();
            tbApply1.setAuditStatus(auditStatus);
            tbApply1.setCreateUser(createUser);
            tbApply1.setIsOpen(isOpen);
            tbApply1.setUpdateUser(updateUser);
            tbApply1.setUserId(userId);
            tbApplyMapper.insert(tbApply1);
            responseEntity.putDataValue("applyId",tbApply1.getApplyId());
            responseEntity.putDataValue("msg","活动审核添加成功");
        }else {
            responseEntity = ResponseEntity.badRequest();
            responseEntity.putDataValue("msg","活动审核添加失败");
        }
        return responseEntity;
    }

    /**
     * 活动审核列表
     * @return
     * @throws IOException
     */
    @ApiOperation("活动审核列表")
    @PostMapping("/web/TbApplyList")
    @ResponseBody
    public ResponseEntity TbApplyList() throws IOException {
        ResponseEntity responseData;
        QueryWrapper<TbApply> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<TbApply> list = tbApplyMapper.selectList(wrapper);
        responseData = ResponseEntity.ok();
        responseData.putDataValue("list",list);
        responseData.putDataValue("msg","成功");
        return responseData;
    }

    /**
     * 根据id删除活动审核
     * @param applyId
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除活动审核")
    @PutMapping("/web/deleteTbApply")
    @ResponseBody
    public ResponseEntity deleteTbApply(@RequestParam(required = true) Integer applyId,@RequestHeader String Authorization)throws IOException{
        ResponseEntity responseEntity;
        String token=Authorization;
        if(flag=myUtil.judgeToken(token)){
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users=JwtToken.unsign(token,TbUserinfo.class);
        if(users==null){
            responseEntity=ResponseEntity.notFound();
            responseEntity.putDataValue("msg","用户不存在");
            return responseEntity;
        }else if(users.getRoleId()==1) {
            TbApply tbApply = tbApplyMapper.selectById(applyId);
            if(tbApply!=null){
                int is_delete = 1;
                tbApply.setIsDelete(is_delete);
                tbApplyMapper.updateById(tbApply);
                responseEntity=ResponseEntity.ok();
                responseEntity.putDataValue("msg","删除成功");
                return responseEntity;
            }
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 根据applyId查询活动审核
     * @param applyId
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据applyId查询活动审核")
    @PutMapping("/web/selectByTbApplyId")
    @ResponseBody
    public ResponseEntity selectByTbApplyId(@RequestParam(required = true) Integer applyId, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()==1) {
            responseEntity=ResponseEntity.ok();
            TbApply tbApply = tbApplyMapper.selectById(applyId);
            responseEntity.putDataValue("tbApply",tbApply);
            return responseEntity;
        }
        responseEntity=ResponseEntity.unauthorized();
        responseEntity.putDataValue("msg","没有权限");
        return responseEntity;
    }

    /**
     * 超级管理员修改活动审核
     * @param applyId
     * @param userId
     * @param isOpen
     * @param auditStatus
     * @param createUser
     * @param updateUser
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("超级管理员修改活动审核")
    @PutMapping("/web/updateTbApply")
    @ResponseBody
    public ResponseEntity updateTbApply(@RequestParam(required = true) int applyId,
                                        //@RequestParam(required = false) int activityId,
                                        @RequestParam(required = false) int userId,
                                        @RequestParam(required = false) int isOpen,
                                        @RequestParam(required = false) String auditStatus,
                                        @RequestParam(required = false) String createUser,
                                        @RequestParam(required = false) String updateUser,
                                             @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        if (flag =myUtil.judgeToken(token)) {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo users = JwtToken.unsign(token, TbUserinfo.class);
        if (users == null) {
            responseEntity = ResponseEntity.notFound();
            responseEntity.putDataValue("msg", "用户不存在");
            return responseEntity;
        } else if (users.getRoleId()!=1) {
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg", "没有权限");
            return responseEntity;

        } else {
            responseEntity = ResponseEntity.ok();
            TbApply tbApply = tbApplyMapper.selectById(applyId);
            tbApply.setAuditStatus(auditStatus);
            tbApply.setCreateUser(createUser);
            tbApply.setIsOpen(isOpen);
            tbApply.setUpdateUser(updateUser);
            tbApply.setUserId(userId);
            tbApplyMapper.updateById(tbApply);
            responseEntity.putDataValue("msg", "活动审核修改成功");
            return responseEntity;
        }
    }

}
